using System;
using NTTHAO.CommonWeb;
using System.Web.UI;

public partial class admin_webparts_Plug_ins_Panel_PanelManagement : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;
        if (action != null)
        {
            if (action.Equals("grouplist", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelGroupList.ascx";
            }
            else if (action.Equals("groupedit", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelGroupInput.ascx";
            }
            else if (action.Equals("groupadd", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelGroupInput.ascx";
            }
            else if (action.Equals("viewgroup", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelList.ascx";
            }
            else if (action.Equals("imageadd", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelInput.ascx";
            }
            else if (action.Equals("imageedit", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "PanelInput.ascx";
            }
            else if (action.Equals("moveimageup", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageUp(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"]);
            }
            else if (action.Equals("moveimagedown", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveImageDown(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"]);
            }
            else if (action.Equals("imagedel", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.DeleteImage(Int32.Parse(Request.QueryString["imageid"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"]);
            }
            else
            {
                controlName = "PanelGroupList.ascx";
            }
        }
        else
        {
            controlName = "PanelGroupList.ascx";
        }

        if (controlName != null)
        {
            Control ctrl = LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
    }

    private void MoveImageUp(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.Panel.Panel detail = new NTTHAO.Web.BL.Panel.Panel(imageID);
            detail.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveImageDown(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.Panel.Panel detail = new NTTHAO.Web.BL.Panel.Panel(imageID);
            detail.MoveDown();
        }
        catch (Exception) { }
    }

    private void DeleteImage(int imageID)
    {
        try
        {
            NTTHAO.Web.BL.Panel.Panel detail = new NTTHAO.Web.BL.Panel.Panel(imageID);
            NTTHAO.Web.BL.Panel.PanelGroup group = new NTTHAO.Web.BL.Panel.PanelGroup(detail.GroupID);

            string imageFileName = "../" + group.ImagePath + detail.FileName;
            detail.Delete();

            if (System.IO.File.Exists(Server.MapPath(imageFileName)))
            {
                System.IO.File.Delete(Server.MapPath(imageFileName));
            }
        }
        catch (Exception) { }
    }
}
